package com.xu.cloud.common.sequence;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 本地原子序列
 *
 * @author xuguan
 * @since 2025/3/28
 */
public class LocalAtomicSequence implements ISequence {

    private static final long MAX = 99999999L;
    private static final NumberFormat fmt = new DecimalFormat("00000000");
    private AtomicLong seq = new AtomicLong(0L);

    public long increment() {
        final long v = seq.incrementAndGet();
        if (v > MAX) {
            seq.set(0L);
        }
        return v;
    }

    @Override
    public String nextId() {
        return fmt.format(increment());
    }
}
